/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bluetoothserver.graphical;

import bluetoothserver.dataStore.AnswerUnits;
import bluetoothserver.dataStore.Question;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Iterator;
import net.sourceforge.chart2d.*;

/**
 *
 * @author Philip
 */
public class BarChart {
    Question question;
    LBChart2D chart;
    int answers = 0;

    private ArrayList<AnswerUnits> results;
    public BarChart(Question q){
        question = q;
        createChart();
    }
    
    public BarChart(Question q, ArrayList<AnswerUnits> ans){
        results = ans;
        question = q;
        createChart();
    }

    private void createChart(){
        //Initializes the chart display
        chart = new LBChart2D();
        chart.setObject2DProperties(Object2DProps());
        chart.setChart2DProperties(chart2DProps());
        chart.setLegendProperties(legendProps());
        chart.setGraphChart2DProperties(graphChart2DProps());
        chart.addGraphProperties(graphProps());
        chart.addDataset(dataSet());
        chart.addMultiColorsProperties(multiColourProps());
        chart.pack();


    }

    private Object2DProperties Object2DProps(){
        Object2DProperties objectProps = new Object2DProperties();
        objectProps.setObjectTitleText(question.getQuestion());
        return objectProps;
    }

    private Chart2DProperties chart2DProps(){
        Chart2DProperties chartProps = new Chart2DProperties();
        return chartProps;
    }

    private LegendProperties legendProps(){
        LegendProperties legendProps = new LegendProperties();
        legendProps.setLegendExistence(false);
        return legendProps;
    }

    private GraphChart2DProperties graphChart2DProps(){
        GraphChart2DProperties graph2DProps = new GraphChart2DProperties();
        String[] answer = new String[results.size()];
        Iterator<AnswerUnits> i = results.iterator();
        int j = 0;
        while(i.hasNext()){
            String s = i.next().getAnswerName();
            answer[j] = s;
            j++;
        }
        graph2DProps.setLabelsAxisLabelsTexts(answer);
        graph2DProps.setGraphComponentsColorsByCat(multiColourProps());
        graph2DProps.setGraphComponentsColoringByCat(true);

        return graph2DProps;
    }

    private GraphProperties graphProps(){
        GraphProperties graphProps = new GraphProperties();
        return graphProps;
    }

    private Dataset dataSet(){
        Dataset dataset = new Dataset(1,results.size(),1);
            Iterator<AnswerUnits> i = results.iterator();
            int j = 0;
            while(i.hasNext()){
                
                AnswerUnits au = i.next();
                Float f  = Float.parseFloat(au.getNumberOfVotes()+"");
                dataset.set(0,j,0,f);
                j++;
            }
        return dataset;
    }

    private MultiColorsProperties multiColourProps(){
        MultiColorsProperties mCProps = new MultiColorsProperties();
        Color[] colours = new Color[results.size()];
        for(int i = 0;i < results.size();i++){
            if(question.getCorrectAnswer() == i){
                colours[i] = Color.GREEN;
            }else{
                colours[i] = Color.RED;
            }
        }
        mCProps.setColorsCustom(colours);
        return mCProps;
    }

    public LBChart2D getChart(Dimension s){
        chart.setMaximumSize(s);
        return chart;
    }
}
